package com.gdpu.api;





import com.github.pagehelper.Page;

import java.util.List;

/**
 * 分页数据封装类
 *
 */
public class CommonPage<T> {
    /**
     * 当前页码
     */
    private Integer pageNum;
    /**
     * 每页数量
     */
    private Integer pageSize;
    /**
     * 总页数
     */
    private Integer totalPage;
    /**
     * 总条数
     */
    private Long total;
    /**
     * 分页数据
     */
    private List<T> list;

    /**
     * 手动封装分页信息
     * @param list
     * @param pageNum
     * @param pageSize
     * @param totalRecord
     * @param <T>
     * @return
     */
    public static <T> CommonPage<T> restPage(List<T> list, Integer pageNum,Integer pageSize,Long totalRecord) {
        CommonPage<T> result = new CommonPage<T>();
        result.setTotalPage( (int) Math.ceil(totalRecord/ pageSize));
        result.setPageNum(pageNum);
        result.setPageSize(pageSize);
        result.setTotal(totalRecord);
        result.setList(list);
        return result;
    }


    /**
     * 将分页后的list转为分页信息
     */
    public static <T> CommonPage<T> restPage(List<T> list, Page page ) {
        CommonPage<T> result = new CommonPage<T>();
        result.setTotalPage((int) page.getPages());
        result.setPageNum((int) page.getPageNum());
        result.setPageSize((int) page.getPageSize());
        result.setTotal(page.getTotal());
        result.setList(list);
        return result;
    }

    /**
     * 将SpringData分页后的list转为分页信息
     */
//    public static <T> CommonPage<T> restPage(org.springframework.data.domain.Page<T> pageInfo) {
//        CommonPage<T> result = new CommonPage<T>();
//        result.setTotalPage(pageInfo.getTotalPages());
//        result.setPageNum(pageInfo.getNumber());
//        result.setPageSize(pageInfo.getSize());
//        result.setTotal(pageInfo.getTotalElements());
//        result.setList(pageInfo.getContent());
//        return result;
//    }

    public Integer getPageNum() {
        return pageNum;
    }

    public void setPageNum(Integer pageNum) {
        this.pageNum = pageNum;
    }

    public Integer getPageSize() {
        return pageSize;
    }

    public void setPageSize(Integer pageSize) {
        this.pageSize = pageSize;
    }

    public Integer getTotalPage() {
        return totalPage;
    }

    public void setTotalPage(Integer totalPage) {
        this.totalPage = totalPage;
    }

    public List<T> getList() {
        return list;
    }

    public void setList(List<T> list) {
        this.list = list;
    }

    public Long getTotal() {
        return total;
    }

    public void setTotal(Long total) {
        this.total = total;
    }
}